/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.impl.DaoRegionImpl;

/*
 * ServletQueryComboConsulta
 *
 * Descripción: Esta clase se encarga de la  consulta select 
 *
 * Autor: Erik
 *
 * Versión: 0.1
 *
 * Fecha: 20/04/14
 *
 */
public class ServletQueryComboConsulta extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String accion = request.getParameter("accion");
        String target = null;
        String result = null;
        //String idPro, String idPPre
        //  System.out.println("CA "+ACA);        
        //  PeruDao peruDao = new PeruDaoImpl();



        try {
            if (accion == null) {
                result = "Solicitud no recibida";

            } else if (accion.equals("EEG")) {
                List<Object[]> list = (new DaoRegionImpl()).query();
                select(list, "id=\"CbRegion\" name=\"CbRegion\"  onchange=\"load_Upd()\" ",
                        response, null, null);
            } else {
                result = "Solicitud no reconocida";
            }
            if (result != null) {
                target = "mensaje.jsp";
                request.getSession().setAttribute("msg", result);
            }

            if (target != null) {
                response.sendRedirect(target);
            }
        } catch (Exception e) {
            System.out.println(" " + e.getMessage());
        }
    }

    public static void select(List<Object[]> list, String atributos,
            HttpServletResponse response, String id, String a) throws Exception {
        StringBuilder sb = new StringBuilder();
        int cont = 0;
        if (list != null) {
            sb.append("<select style=\"width:222px\" ").append(atributos).append(">");
            for (Object[] fila : list) {

                if (a != null && cont == 0) {
                    sb.append("<option  ");
                    sb.append(" value=\"").append("-1").append("\">");
                    sb.append("seleccionar todos");
                    sb.append("</option>");
                    cont++;
                }
                sb.append("<option  ");
                if (id != null) {
                    if (id.equals(fila[0].toString())) {
                        sb.append(" selected ");
                    }
                }
                sb.append(" value=\"").append(fila[0]).append("\">");
                sb.append(fila[1]);
                sb.append("</option>");
            }
            sb.append("</select>");
        }

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(sb);
        out.close();
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
